//
//  ViewController.m
//  05-UIScrollView
//
//  Created by 王道林 on 16/7/20.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "ViewController.h"

//宏定义  命名俩种方式
// 一. 全大写
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
// 二. k + 驼峰命名
#define kScreenWidth [UIScreen mainScreen].bounds.size.width


@interface ViewController ()<UIScrollViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    scrollView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:scrollView];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(10, 10, WIDTH - 20, 100);
    button.backgroundColor = [UIColor redColor];
    [scrollView addSubview:button];
    
    
    //1. contentSize 设置可以滚动的范围  默认CGSizeZero 等效于 CGSizeMake(0, 0)
    scrollView.contentSize = CGSizeMake(WIDTH * 4, 1000);
    
    // 2. contentOffSet 偏移量  默认值CGPointZero
    //  不设置contentSize , contentOffset也可以改变
    //  改变contentOffSet , 实质上是对scrollView的bounds进行修改
    scrollView.contentOffset = CGPointMake(0, 500);
    
    // 3.contentInset
    //  添加滚动区域四周的滚动范围
    scrollView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    // 4. directionalLockEnabled
    // 锁定垂直或者水平滚动.. (45°角 ,锁会失效)
    scrollView.directionalLockEnabled = YES;
    
    // 5.  bounces  回弹效果  边缘弹动动画
    scrollView.bounces = YES;
    
    // 6.alwaysBounceVertical
    //  在没有垂直方向的滚动范围时   保证垂直方向 有回弹效果
    scrollView.alwaysBounceVertical = YES;
    
    //7 .pagingEnabled 按页滚动
    scrollView.pagingEnabled = YES;
    // 8. scrollEnabled 滚动视图能否滚动.  默认yes
    scrollView.scrollEnabled = YES;
    
    // 9 . 是否显示垂直/ 水平的滚动指示器 , 默认 YES
    scrollView.showsVerticalScrollIndicator = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    
    // 10 .  滚动指示器的边框
    scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(100, 0, 0, 0);
    
    //11 . pagingEnabled.  滑动指示器的颜色
    scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    
    //tracking 用户是否开始拖动
 //   scrollView.tracking;
    // dracking 用户是否正在拖动
 //   scrollView.dragging;
    //decelerating 是否 减速中
//    scrollView.decelerating
    
    // 14. 双击状态栏  回到顶部
    scrollView.scrollsToTop = YES;
    
    
    // 15. delegate
    scrollView.delegate = self;
    
    // 16. 键盘消失模式
    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    // 测试:
    UITextField *textFiled = [[UITextField alloc] initWithFrame:CGRectMake(10, 300, WIDTH - 20, 200)];
    textFiled.backgroundColor = [UIColor greenColor];
    [scrollView addSubview:textFiled];
    
    // 17. 缩放系数
    // 如果想进行视图放大缩小,  必须实现viewForZoomingInScrollView
    scrollView.minimumZoomScale = 0.5;
    scrollView.maximumZoomScale = 2.0;
    
    
    
}


// 与其想要缩放的子视图绑定, scrollView 的contentSize会
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return [scrollView.subviews firstObject];
}









// 滚动视图, 就会执行
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // tracking 用户触摸
    if (scrollView.isDragging) {
        NSLog(@"正在拖动");
    }
    // dragging  用户开始滑动
    if (scrollView.dragging) {
        NSLog(@"开始滑动");
    }
    // decelerating 用户触摸结束
    if (scrollView.decelerating) {
        NSLog(@"");
    }
}

// 将要开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSLog(@"开始拖拽");
}


// 已经结束拖拽

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    NSLog(@"结束拖拽");
}

// 将要开始减速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"开始减速");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
